$(document).ready(function(){
    var positionResult = false;
    fillDepart();
    $("#department").bind('change',changeDate);

    function fillDepart(){
        var arr = [];
        var op='<option value="0">请选择</option>';
        arr.push(op);
        for (var i in bu_name_list){
            var str = '<option value="'+bu_name_list[i].bu_id+'">'+bu_name_list[i].bu_name+'</option>';
            arr.push(str);
        }
        $("#department").html(arr.join(''));
    }

    function changeDate(){
        var obj = $(this);
        var key = obj.val();
        var html='';
        html+='<tr>';
        html+='<td class="td1"><input type="checkbox" name="" id="all"/>全选</td>';
        html+='<td class="td2">部门</td>';
        html+='</tr>';
        for(var i in bu_info[key]){
            html+='<tr>';
            html+='<td class="td1"><input type="checkbox" name="city[]" value="'+i+'"/>'+city_list[i]+'</td>';
            html+='<td class="td2">';
            $.each(bu_info[key][i],function(i,v){
                html+='<input type="checkbox" value="'+v.department_id+'" name="department_ids[]"/>'+v.department_name+'';
            });
            html+='</td>';
            html+='</tr>';
        }
        $("#t1").html(html);
        $("#t1").css({'display':"block"});
        $.each($("#t1 tr:last"),function(i,v){
            $.each($(v).find("td"),function(i,v){
                $(v).css({'border-bottom':'none'});
            });
        });

        allCheck();
    }
    function allCheck(){
        //全选
        $("#all").bind("click",function(){
            if($(this).attr("checked")){
                $.each($("#t1 [type=checkbox]"),function(i,v){
                    $(v).attr("checked",'checked');
                });
            }else{
                $.each($("#t1 [type=checkbox]"),function(i,v){
                    $(v).removeAttr("checked");
                });
            }
        });

        //根据城市选择部门
        $(".td1 [type=checkbox]").bind("click",function(i,v){
            var all=$(this).parent().parent();
            if($(this).attr("checked")){
                $.each(all,function(i,v){
                    $.each($(v).find("input"),function(i,v){
                        $(v).attr("checked",'checked');
                    });
                });
            }else{
                $.each(all,function(i,v){
                    $.each($(v).find("input"),function(i,v){
                        $(v).removeAttr("checked");
                    });
                });
            }
        });
    }
    $("input[name='userName']").autocomplete({
        source: function(request, response) {
            $.ajax({
                url:url_user,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        return {
                            label:  item.name ,
                            value:  item.name,
                            job_name: item.job_name,
                            user_id:  item.user_id,
                            job_id :item.job_id
                        }

                    }));
                }
            });
        },
        select: function( event, ui ) {
            var user_id = ui.item.user_id;
            var job_id = ui.item.job_id;
            $("#uid").val(user_id);
            $.ajax({
                url:url_role,
                dataType: "json",
                data: {
                    user_id: user_id
                },
                success: function(data) {
                    var data=eval(data);
                    if(data.data){
                        fillDate(data.data);
                    }

                }
            });
        }
    });

    /**
     * 渲染已有权限
     * @param data
     */
    function fillDate(data){
        var source='',html='',title,foot,tit;
            title="<div class='permission' ><label class='permissionName'>已有权限：</label>";
            foot="<label class='perControl' id='deldata'><a href='javascript:void(0)'>删除</a></label></div>";

        $.each(data,function(i,v){
            if(i!="name_list"){
                html+="<div>";
                html+="<span class='sp1' id='"+i+"'>"+data['name_list'][i]+"</span>";
                html+="<div class='dd1'>";
                for(var sour in data[i]){
                    html+="<span id='permission"+sour+"'><input type='checkbox' name='permission["+sour+"]' id='"+sour+"'>"+data[i][sour]+"</span>";
                }
                html+="</div>";
                html+="</div>";
            }

        });
        source=source+html;
        tit=title+source+foot;
        $("#permission").html(tit);
        delData();
    }

    /**
     * 绑定删除事件
     */
    function delData(){
        $("#deldata").bind("click",function(){
            var arr='',str={};
            $.each($("#permission input"),function(i,v){
                if($(v).attr("checked")){
                    arr=arr+$(v).attr('id')+',';
                    str[i]=($(v).attr('id'));
                }
            });
            var user_id=$('#uid').val();
            $.ajax({
                url:url_role,
                dataType: "json",
                data:{id:arr,user_id:user_id},
                success: function(data) {
                    var da=eval(data);
                    if(da.errcode===1){
                        for(var val in str){
                            $("#permission"+str[val]).remove();
                        }
                    }
                }
            });
        });
    }

    $("input[name='parent_name']").bind("keydown",function(event){
        if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
            return;
        }
        positionResult = false;
    });
    $("input[name='parent_name']").bind("blur",function(){
        if(!positionResult){
            $(".leader_id").val("");
            $(".leader_job").val("");
            $(this).val("");

        }

    });
    $("#cityAll").bind('click',function(){
        var obj = $(this);
        var result = !!obj.attr('checked');
        if(result){
            $("input[name='city[]']").attr("checked","checked");
            return;
        }
        $("input[name='city[]']").attr("checked",false);


    });


});